// VBConversions Note: VB project level imports
using System.Data;
using DevExpress.XtraEditors.Controls;
using System.Diagnostics;
using System.Xml.Linq;
using DevExpress.XtraEditors.Repository;
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.VisualBasic;
using System.Configuration;
using System.Collections;
using DevExpress.XtraEditors;
using System.Data.Sql;
using Microsoft.SqlServer;
using System.Windows.Forms;
using System;
using DevExpress.XtraReports;
using Microsoft.SqlServer.Management.Smo;
using DevExpress;
using System.Linq;
using System.IO;
// End of VB project level imports


namespace SCIEF.SysBFW.CommonExt
{
	public class MetaValue
	{
		public string ApplicationName {get; set;}
		public Guid MetaValueID {get; set;}
		public string Notes {get; set;}
		public bool SystemRecord {get; set;}
		public string Value {get; set;}
		public string ValueDescr_ALT {get; set;}
		public string ValueDescr_PRM {get; set;}
		public string ValueID {get; set;}
		
		public static MetaValue GetMetaValueByCode(string code)
		{
			const string selectStatement = "SELECT MetaValueID, ApplicationName, ValueID, Value, ValueDescr_PRM, ValueDescr_ALT, Notes, SystemRecord FROM dbo.MetaValues WHERE ValueID = \'{0}\'";
			return GetMetaValue(string.Format(selectStatement, code));
		}
		
		public static MetaValue GetMetaValueById(Guid id)
		{
			const string selectStatement = "SELECT MetaValueID, ApplicationName, ValueID, Value, ValueDescr_PRM, ValueDescr_ALT, Notes, SystemRecord FROM dbo.MetaValues WHERE MetaValueID = \'{0}\'";
			return GetMetaValue(string.Format(selectStatement, id.ToString()));
		}
		
		private static MetaValue GetMetaValue(string selectStatement)
		{
			SqlCommand command = Base.Connection.Command();
			SqlDataReader reader = null;
			DataTable dtrow = new DataTable();
			DataRow dr = null;
			//
			MetaValue mv = null;
			
			try
			{
				command.CommandText = selectStatement;
				reader = command.ExecuteReader(CommandBehavior.CloseConnection);
				if (reader.HasRows)
				{
					dtrow.Load(reader);
					dr = dtrow.Rows[0];
					mv = new MetaValue();
					//Pk always there
					mv.MetaValueID = (Guid) (dr["MetaValueID"]);
					//
					mv.ValueID = dr["ValueID"].ToString();
					mv.Value = dr["Value"].ToString();
					mv.ValueDescr_ALT = dr["ValueDescr_ALT"].ToString();
					mv.ValueDescr_PRM = dr["ValueDescr_PRM"].ToString();
					mv.Notes = dr["Notes"].ToString();
					mv.SystemRecord = System.Convert.ToBoolean(dr["SystemRecord"]);
				}
			}
			catch (Exception)
			{
				throw (new Exception(string.Format("Invalid SQL command: MetaValue.GetMetaValueByCode{0} {1}", "\r\n", command.CommandText)));
			}
			finally
			{
				if (reader != null)
				{
					reader.Close();
				}
				dtrow.Dispose();
				command.Dispose();
			}
			return mv;
		}
		
	}
	
}
